import { defineStore } from 'pinia';
import {UserApi} from "@/api/user/index.js";
import authData from '../mock/authData.js';
import {ElMessage} from "element-plus";
/**
 * 用户认证状态管理库
 * 负责处理用户登录、注册、注销及个人信息管理
 */
export const useUserStore = defineStore('user', {
    state: () => ({
        userList: [],
        pageInfo: {
            pageSize: 10,
            pageNum: 1,
            total: 0,
            totalPage: 0
        },
        ssmcode: ''   ,  //验证码
        authPendingList: [],
        authStatus: [],
        userProfile:{}
    }),
    actions: {
       async getUserList() {
            const res = await UserApi.getUserList({
                page: this.pageInfo.pageNum,
                size: this.pageInfo.pageSize
            });
            this.userList = res.data.records;
            this.pageInfo.pageSize = res.data.pageSize;
            this.pageInfo.pageNum = res.data.pageNumber;
            this.pageInfo.total = res.data.totalRow;
            this.pageInfo.totalPage = res.data.totalPage;
        },

        //更新用户信息
        async updateUserInfo(user) {
            const res = await UserApi.updateUserInfo(user);
            if(res.code == 200){
                ElMessage.success('更新成功');
                this.getUserList();
            }else{
                ElMessage.error('更新失败');
            }
        },
        //删除用户
        async deleteUser(id) {
            const res = await UserApi.deleteUser(id);
            if(res.code == 200){
                ElMessage.success('删除成功');
                this.getUserList();
            }else{
                ElMessage.error('删除失败');
            }
        },
        //获取用户详情
        async getUserDetail(id) {
            const res = await UserApi.getUserDetail(id);
            return res.data;
        },
        //获取验证码
        async getSsmCode(phone) {

            const res = await UserApi.getSsmCode(phone);
            if(res.code == 200){
                ElMessage.success('获取验证码成功');
                this.ssmcode = res.data;
                console.log('ssmcode', this.ssmcode)
            }else{
                ElMessage.error('获取验证码失败');
            }
        },
        //更换绑定手机号
        async updatePhone(data) {
            const res = await UserApi.updatePhone(data);
            this.ssmcode = '';
            if(res.code == 200){
                ElMessage.success('更换绑定手机号成功');
            }else{
                ElMessage.error('更换绑定手机号失败');
            }
        },

        //实名认证
        async authSubmit(data) {
            const res = await UserApi.authSubmit(data);
            if(res.code == 200){
                ElMessage.success('实名认证成功');
            }else{
                ElMessage.error('实名认证失败');
            }
        },
        //获取待审核实名认证表
        async getAuthPendingList() {
            const res = await UserApi.getAuthPendingList();
            this.authPendingList = res.data;
            if(res.code == 200){
                ElMessage.success('获取待审核实名认证列表成功');
            }else{
                ElMessage.error('获取待审核实名认证列表失败');
            }
        },

        removePendingItem(id) {
            this.authPendingList = this.authPendingList.filter(item => item.id !== id);
        },
        //审核实名认证
        async authPending(data) {
            const res = await UserApi.authPending(data);
            if(res.code == 200){
                ElMessage.success('审核实名认证成功');
            }else{
                ElMessage.error('审核实名认证失败');
            }
        },
        //获取实名认证状态
        async getAuthStatus() {
            const res = await UserApi.getAuthStatus();
            console.log('实名认证res',res)
            this.authStatus = res.data;
            if(res.code == 200){
                ElMessage.success('获取实名认证状态成功');
            }else{
                ElMessage.error('获取实名认证状态失败');
            }
        },
        //实名认证审核
        async authVerify(data) {
            const res = await UserApi.authVerify(data);
            if(res.code == 200){
                ElMessage.success('实名认证审核成功');
            }else{
                ElMessage.error('实名认证审核失败');
            }
        },
        //获取用户信息
        async getUserProfile() {
            const res = await UserApi.getUserProfile();
            console.log('===================================')
            console.log('res',res)
            this.userProfile = res.data;
            if(res.code == 200){
                ElMessage.success('获取用户信息成功');
            }else{
                ElMessage.error('获取用户信息失败');
            }
        },
        //更新个人信息
        async updateUserProfile(data) {
            const res = await UserApi.updateUserProfile(data);
            if(res.code == 200){
                ElMessage.success('更新个人信息成功');
            }else{
                ElMessage.error('更新个人信息失败');
            }
        },
        //修改密码
        async updatePassword(data) {
            const res = await UserApi.updatePassword(data);
            if(res.code == 200){
                ElMessage.success('修改密码成功');
            }else{
                ElMessage.error('修改密码失败');
            }
        }
    }
})